#!/usr/bin/perl
# Plot curve of NIPU value along sequence
# Author: Yu Zhou
# 2007

use strict;
use warnings;
use Carp;
use GD::Graph::Data;
use GD::Graph::linespoints;
use Data::Dumper;

if ($#ARGV < 1) {
    die "Usage: plotline.pl infile outfile";
}

my $infile  = $ARGV[0]; 
my $outfile = $ARGV[1];

open my $FH, $infile 
    or die "Couldn't open file $infile: $!";

my @yvalues = ();
my $num = 1;
while (my $line = <$FH>) {
    chomp($line);
	my ($pos, $pu) = split " ", $line;
	push @yvalues, $pu;
}
close($FH);
 
my $num_values = $#yvalues + 1;
my (@xvalues) = (1 .. $num_values);

my $data = GD::Graph::Data->new([ [@xvalues], [@yvalues] ]);
my $values = $data->copy;

# create new image
my $WIDTH_PER_POSITION = 15;
my $width  = $WIDTH_PER_POSITION * $num_values;
my $height = 300; 

my $graph = new GD::Graph::linespoints($width, $height);
    
# discover maximum values of x and y for graph parameters
my $xmax = $num_values;
my $ymax = 1.0;
my $yticks = 10;

# set parameters for graph
$graph->set(
	transparent       => 0,
	title             => "Single-strandedness",
	x_label           => 'Position',
	y_label           => 'PU value',
	x_all_ticks       => 1,
	y_all_ticks       => 0,
	y_tick_number     => $yticks,
	x_max_value       => $xmax,
	y_max_value       => $ymax,
	zero_axis         => 1,
	show_values       => $values,
	marker_size       => 3,
	markers           => [1]
);
    
# plot the data on the graph
my $gd = $graph->plot($data);

# output file
my $pngfile = $outfile.".png"; 
unless(open(PNG, ">$pngfile")) {
	croak "Cannot open $pngfile:$!\n";
}
binmode PNG;

print PNG $gd->png;

